﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using mBlog.Core.Services;
using mBlog.Data;
using mBlog.Core.Extensions;
using mBlog.Core;
using mBlog.Core.Models;

namespace mBlog.Test
{
    [TestClass]
    public class PostServiceTest
    {
        //[TestMethod]
        //public void PostServiceCanGetPostByID()
        //{
        //    mBlogContext blogContext = new mBlogContext();

        //    IPostService pService = new PostService(
        //            new Repository<Post>(blogContext),
        //            new Repository<Category>(blogContext),
        //            new Repository<Tag>(blogContext)
        //        );

        //    var post = pService.Single(14);

        //    Assert.IsNotNull(post);

        //    Assert.AreEqual(2, post.Comments.Count);
        //    Assert.AreEqual(1, post.Categories.Count);
        //}

        public PostServiceTest()
        {
            ModelEntityMapperConfiguration.Init();
        }

        [TestMethod]
        public void PostServiceAddTest()
        {
            mBlogContext blogContext = new mBlogContext();

            IPostService pService = new PostService(
                    blogContext
                );

            Post p = new Post();

            var savedPost = pService.Single("fengqijun1");
            if (savedPost == null)
            {
                p.Title = "Post service test again";
                p.UrlName = "fengqijun1";
                p.Excerpt = "post service test";
                p.Content = "添加测试";
                p.PostedTime = DateTime.Now;
                p.IsPublished = false;


                ICategoryService catService = new CategoryService(new mBlogContext());

                var category = catService.Single(36);
                p.Categories = new List<Category>();
                p.Categories.Add(category);

                p.Categories.Add(new Category()
                    {
                        Name = "category test",
                        UrlName = "category-test",
                        AddedTime = DateTime.Now,
                        IsActive = true
                    });

                pService.Add(p);
            }
            var retrievdPost = pService.Single("fengqijun1");
            Assert.IsNotNull(retrievdPost);

            Assert.IsNotNull(retrievdPost.Categories);
            Assert.AreEqual(1, retrievdPost.Categories.Count);
        }

        [TestMethod]
        public void PostServiceUpdateTest()
        {
            mBlogContext blogContext = new mBlogContext();

            IPostService pService = new PostService(
                    blogContext
                );

            var post = pService.Single("fengqijun1");
            post.Title = "Update Successfully2";
            
            pService.Update(post);
            var newPost = pService.Single("fengqijun1");

            Assert.AreEqual("Update Successfully2", newPost.Title);

        }

        [TestMethod]
        public void PostServiceUpdateCategoryTest()
        {
            mBlogContext blogContext = new mBlogContext();

            IPostService pService = new PostService(
                    blogContext
                );

            var post = pService.Single("fengqijun1");

            ICategoryService catService = new CategoryService(blogContext);
            var category = catService.Single(34);
            post.Categories.Clear();
            post.Categories.Add(category);

            pService.Update(post);

            var newPost = pService.Single("fengqijun1");

            Assert.AreEqual(34, newPost.Categories[0].ID);
        }

        [TestMethod]
        public void PostServiceGetByCategoryTest()
        {
            mBlogContext blogContext = new mBlogContext();

            IPostService pService = new PostService(
                   blogContext
                );

            var posts = pService.GetByCategory("web", 0, 10);

            Assert.IsNotNull(posts);
            Assert.AreEqual(3, posts.Count);
        }

        [TestMethod]
        public void PostServiceAddComment()
        {
            mBlogContext blogContext = new mBlogContext();

            IPostService pService = new PostService(
                    blogContext
                );

            var p = pService.Single(210);

            var comment = 
                    new Core.Models.Comment()
                    {
                        IP = "127.0.0.1",
                        Author = "Derek",
                        Content = "Post Service Comment Add Test",
                        IsSpam = false,
                        Email = "derek.feng@gmail.com",
                        CommentTime = DateTime.Now,
                        Site = "http://fengqijun.com"
                    };


            pService.AddComment(210, comment);

            Assert.AreEqual(1, pService.Single(210).Comments.Count);
        }

        [TestMethod]
        public void DeleteTagTest()
        {
            mBlogContext blogContext = new mBlogContext();

            IPostService pService = new PostService(
                    blogContext
                );

            var post = pService.Single(49);

            Assert.AreEqual(1, post.Tags.Count);

            //var p = blogContext.Posts.Single(po => po.Id == 49);
            //var t = blogContext.Tags.Single(ta => ta.Id == 1);
            //p.Tags.Remove(t);

            //blogContext.SaveChanges();

            pService.DeleteTag(49, 4);

            var p = pService.Single(49);

            Assert.AreEqual(0, p.Tags.Count);
        }
    }
}
